//
//  YZXPhotoPickerController.swift
//  YZXWeibo
//
//  Created by 叶周翔 on 16/7/31.
//  Copyright © 2016年 叶周翔. All rights reserved.
//

import UIKit

class YZXPhotoPickerController: UICollectionViewController,YZXPhotoCellDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    var sendBtnBeUse:(() -> Void)?
    
    var photos: [UIImage] = [UIImage]()
    // 布局属性
    private let layout = UICollectionViewFlowLayout()
    
    private let reuseIdentifier = "cell"
    /// 最大张数
    private let maxPhotoCount = 6
    //MARK:-view的生命周期
    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.backgroundColor = UIColor.whiteColor()
        // 注册一个cell
        self.collectionView?.registerClass(YZXPhotoCell.classForCoder(), forCellWithReuseIdentifier: reuseIdentifier)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    init() {
        super.init(collectionViewLayout: layout)
        
        self.setupCollectonView()
    }
    // MARK:-
    // 设置collectionView
    private func setupCollectonView (){
        // 设置itemSize的大小
        self.layout.itemSize = CGSize(width: 80.0, height: 80.0)
        self.collectionView?.contentInset = UIEdgeInsets(top: 10, left: 10, bottom: 0, right: 10)
    }
}

extension YZXPhotoPickerController {
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let count = self.photos.count == maxPhotoCount ? photos.count : photos.count+1
        return count
    }
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! YZXPhotoCell
        
        cell.backgroundColor = UIColor.brownColor()
        if indexPath.item < photos.count {
            let image = photos[indexPath.item]
            cell.image = image
        }else{
            // cell要显示加号按钮
            cell.setupAddBtn()
        }
        // 设置代理
        cell.delegate = self
        return cell
    }
}
// cell的自定义代理方法
extension YZXPhotoPickerController {
    func photoCellDidClickAddButton(cell: YZXPhotoCell) {
        // 弹出对应的照片选择器
        let picker = UIImagePickerController()
        picker.delegate = self
        self.presentViewController(picker, animated: true, completion: nil)
    }
    func photoCellDidClickDeleteButton(cell: YZXPhotoCell) {
        // 获取按钮点击的cell 对应的indexPath
        let indexPath = collectionView?.indexPathForCell(cell)
        // 删除数组中对应的图片
        photos.removeAtIndex(indexPath!.item)
        // 刷新数据
        self.collectionView?.reloadData()
    }
}
// 图片选择器的代理方法
extension YZXPhotoPickerController {
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        
        let newImage = image.scaleImage()
        // 添加图片
        photos.append(newImage)
        self.sendBtnBeUse!()
        
        self.collectionView?.reloadData()
        picker.dismissViewControllerAnimated(true, completion: nil)
    }
}



